#!/bin/bash
# ========================================
# RedFire MySQL用户初始化脚本
# 版本: v5.0.0 (2025-09-16)
# 功能: 创建业务用户和权限配置
# ========================================

set -e

echo "=========================================="
echo "开始初始化RedFire MySQL用户和权限..."
echo "=========================================="

# 等待MySQL完全启动
echo "等待MySQL服务启动..."
while ! mysqladmin ping -h localhost -u root -p"${MYSQL_ROOT_PASSWORD}" --silent; do
    echo "等待MySQL启动中..."
    sleep 2
done
echo "MySQL服务已启动！"

# 连接到MySQL
mysql -u root -p"${MYSQL_ROOT_PASSWORD}" <<EOF

-- ========================================
-- 创建业务用户
-- ========================================

-- 创建应用用户 (读写权限)
CREATE USER IF NOT EXISTS 'redfire_app'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
CREATE USER IF NOT EXISTS 'redfire_app'@'localhost' IDENTIFIED BY '${MYSQL_PASSWORD}';

-- 创建只读用户 (查询权限)
CREATE USER IF NOT EXISTS 'redfire_readonly'@'%' IDENTIFIED BY 'redfire_readonly_2025';
CREATE USER IF NOT EXISTS 'redfire_readonly'@'localhost' IDENTIFIED BY 'redfire_readonly_2025';

-- 创建监控用户 (监控权限)
CREATE USER IF NOT EXISTS 'redfire_monitor'@'%' IDENTIFIED BY 'redfire_monitor_2025';
CREATE USER IF NOT EXISTS 'redfire_monitor'@'localhost' IDENTIFIED BY 'redfire_monitor_2025';

-- 创建备份用户 (备份权限)
CREATE USER IF NOT EXISTS 'redfire_backup'@'%' IDENTIFIED BY 'redfire_backup_2025';
CREATE USER IF NOT EXISTS 'redfire_backup'@'localhost' IDENTIFIED BY 'redfire_backup_2025';

-- 创建复制用户 (主从复制权限)
CREATE USER IF NOT EXISTS 'redfire_repl'@'%' IDENTIFIED BY 'redfire_repl_2025';
CREATE USER IF NOT EXISTS 'redfire_repl'@'localhost' IDENTIFIED BY 'redfire_repl_2025';

-- ========================================
-- 授予权限
-- ========================================

-- 应用用户权限 (完整的业务操作权限)
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON redfire_trading.* TO 'redfire_app'@'%';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON redfire_trading.* TO 'redfire_app'@'localhost';

-- 只读用户权限 (查询和视图权限)
GRANT SELECT, SHOW VIEW ON redfire_trading.* TO 'redfire_readonly'@'%';
GRANT SELECT, SHOW VIEW ON redfire_trading.* TO 'redfire_readonly'@'localhost';

-- 监控用户权限 (监控相关权限)
GRANT SELECT ON *.* TO 'redfire_monitor'@'%';
GRANT SELECT ON *.* TO 'redfire_monitor'@'localhost';
GRANT PROCESS ON *.* TO 'redfire_monitor'@'%';
GRANT PROCESS ON *.* TO 'redfire_monitor'@'localhost';
GRANT REPLICATION CLIENT ON *.* TO 'redfire_monitor'@'%';
GRANT REPLICATION CLIENT ON *.* TO 'redfire_monitor'@'localhost';

-- 备份用户权限 (备份相关权限)
GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES ON redfire_trading.* TO 'redfire_backup'@'%';
GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES ON redfire_trading.* TO 'redfire_backup'@'localhost';
GRANT RELOAD, PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'redfire_backup'@'%';
GRANT RELOAD, PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'redfire_backup'@'localhost';

-- 复制用户权限 (主从复制权限)
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'redfire_repl'@'%';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'redfire_repl'@'localhost';

-- ========================================
-- 刷新权限
-- ========================================

FLUSH PRIVILEGES;

-- ========================================
-- 验证用户创建
-- ========================================

SELECT 
    User as '用户名',
    Host as '主机',
    account_locked as '是否锁定',
    password_expired as '密码是否过期'
FROM mysql.user 
WHERE User LIKE 'redfire_%'
ORDER BY User, Host;

EOF

echo "=========================================="
echo "RedFire MySQL用户初始化完成！"
echo "创建的用户："
echo "  - redfire_app: 应用用户 (读写权限)"
echo "  - redfire_readonly: 只读用户 (查询权限)"
echo "  - redfire_monitor: 监控用户 (监控权限)"
echo "  - redfire_backup: 备份用户 (备份权限)"
echo "  - redfire_repl: 复制用户 (复制权限)"
echo "=========================================="
